package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeMyAddress;
import com.htgd.gardenexpo.entity.GeMyInformation;
import com.htgd.gardenexpo.page.AddressQuery;
import com.htgd.gardenexpo.page.InformationQuery;
import com.htgd.gardenexpo.service.GeMyAddressService;
import com.htgd.gardenexpo.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 常用地址 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-22
 */
@RestController
@RequestMapping("/api/address")
@Slf4j
@Api(tags = "常用地址")
public class GeMyAddressController {

    @Autowired
    private GeMyAddressService geMyAddressService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add (@RequestBody GeMyAddress geMyAddress,
                                         HttpServletRequest request){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            geMyAddress.setOpenId(openId);
            geMyAddress.setCreateTime(LocalDateTime.now());
            geMyAddressService.save(geMyAddress);
            //默认地址
            if(geMyAddress.getStatus()==1){
                geMyAddressService.changeAllStatus(geMyAddress.getId());
            }
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit (@RequestBody GeMyAddress geMyAddress){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geMyAddress.setUpdateTime(LocalDateTime.now());
            geMyAddressService.updateById(geMyAddress);
            //默认地址
            if(geMyAddress.getStatus()==1){
                geMyAddressService.changeAllStatus(geMyAddress.getId());
            }
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete (Integer id){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeMyAddress geMyAddress = new GeMyAddress();
            geMyAddress.setId(id);
            geMyAddress.setStatus(0);
            geMyAddress.setDeleteFlag("1");
            geMyAddressService.updateById(geMyAddress);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeMyAddress>> list(@RequestBody AddressQuery addressQuery,
                                                HttpServletRequest request) {
        CommonResult<List<GeMyAddress>> result = new CommonResult<>();
        try {
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            addressQuery.setOpenId(openId);
            PageInfo<GeMyAddress> list = geMyAddressService.queryList(addressQuery);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeMyAddress> details(Integer id) {
        CommonResult<GeMyAddress> result = new CommonResult<>();
        try {
            GeMyAddress geMyAddress = geMyAddressService.getById(id);
            geMyAddress.setOpenId("");
            result.setCode(200);
            result.setData(geMyAddress);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
